import React, { useState, useEffect } from "react";
import { Space, Table, Button } from 'antd';
import { getActivityDataApi, checkActivityApi } from '../../http/api/activity';

export default function ActivityList() {
  const [currentPage] = useState(1);
  const [pageSize] = useState(5);
  const [activityData, setActivityData] = useState([]);

  // 审核
  const onCheckActivity = (item) => {
    console.log(item);

    checkActivityApi({
      id: item.activity_id,
      state: 1,
      openid: item.openid,
      activityTitle: item.activity_title,
      activityTime: item.activity_detasel
    })
      .then(res => {
        console.log('审核结果：', res.data)
      })
      .catch(err => {
        console.log('审核失败：', err);
      })
  }
  const columns = [
    {
      title: '活动标题',
      dataIndex: 'activity_title',
      key: 'activity_title',
      render: (text) => <a>{text}</a>,
    },
    {
      title: '时间',
      dataIndex: 'activity_detasel',
      key: 'activity_detasel',
    },
    {
      title: '地点',
      dataIndex: 'activity_name',
      key: 'activity_name',
    },
    {
      title: '操作',
      key: 'action',
      render: (record) => (
        <Space size="middle">
          <Button onClick={() => onCheckActivity(record)}>审核</Button>
          <Button>查看</Button>
          <Button>删除</Button>
        </Space>
      ),
    },
  ];

  function getInitData(currentPage, pageSize) {
    getActivityDataApi(currentPage, pageSize)
      .then(res => {
        console.log('活动数据：', res.data);
        if (res.data.code == '200') {
          setActivityData(res.data.data)
        }
      })
      .catch(err => {
        console.log('活动数据失败：', err)
      })
  }

  useEffect(() => {
    getInitData(currentPage, pageSize);
  }, [])

  const rowSelection = {
    onChange: (selectedRowKeys, selectedRows) => {
      console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
    },
    onSelect: (record, selected, selectedRows) => {
      console.log(record, selected, selectedRows);
    },
    onSelectAll: (selected, selectedRows, changeRows) => {
      console.log(selected, selectedRows, changeRows);
    },
  };

  return (
    <div className="activity_list">
      <Table rowSelection={rowSelection} columns={columns} dataSource={activityData} />
    </div>
  )
}
